package com.ittest.web.controller.store;

import com.github.pagehelper.PageInfo;
import com.ittest.domain.store.Orders;
import com.ittest.service.store.OrdersService;
import com.ittest.service.store.impl.OrdersServiceImpl;
import com.ittest.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/store/orders/*")
public class OrdersServlet extends BaseServlet {
    private OrdersService ordersService = new OrdersServiceImpl();

    //删除
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        //调用service方法
        ordersService.delete(id);
        //重定向到分页查询操作
        resp.sendRedirect(req.getContextPath()+"/store/orders/findAll");
    }

    //查看详情
    protected void toShow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取id
        String id = req.getParameter("id");
        //根据id查询企业对象
        Orders orders = ordersService.findById(id);
        //保存数据到页面
        req.setAttribute("orders",orders);
        //跳转到页面
        req.getRequestDispatcher("/pages/store/orders/orders-show.jsp").forward(req,resp);
    }

    //分页查询订单
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page =1 ;
        int size =5 ;

        if (StringUtils.isNotBlank(req.getParameter("page"))){
            page=Integer.parseInt(req.getParameter("page"));
        }
        if (StringUtils.isNotBlank(req.getParameter("size"))){
            size=Integer.parseInt(req.getParameter("size"));
        }
        //调用service层,查询数据
        PageInfo pageInfo = ordersService.findAll(page, size);
        //数据存到request域中
        req.setAttribute("size",size);
        req.setAttribute("page",pageInfo);
        //跳转到页面
        req.getRequestDispatcher("/pages/store/orders/orders-list.jsp").forward(req,resp);
    }

}
